package com.sp.scmdb.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.SecurityTrainingBean;
import com.sp.scmdb.bean.SecurityTrainingTypeBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.User;
import com.sp.scmdb.service.SecurityTrainingService;
import com.sp.scmdb.service.SecurityTrainingTypeService;

@Controller("securityTrainingAction")
@Scope("prototype")
public class SecurityTrainingAction extends BaseAction<SecurityTrainingBean> {
	private static final long serialVersionUID = 1905892983894850L;
	private static Log log = LogFactory.getLog(SecurityTrainingAction.class);
	
	private SecurityTrainingService securityTrainingService;
	private SecurityTrainingTypeService securityTrainingTypeService;
	
	@Autowired
	public void setSecurityTrainingService(SecurityTrainingService securityTrainingService) {
		this.securityTrainingService = securityTrainingService;
	}

	@Autowired
	public void setSecurityTrainingTypeService(SecurityTrainingTypeService securityTrainingTypeService) {
		this.securityTrainingTypeService = securityTrainingTypeService;
	}

	public String findAll() {
		try {
			List<SecurityTrainingBean> securityTrainingList = securityTrainingService.getListSecurityTraining(null);
			entity.setAllList(securityTrainingList);
			entity.setList(securityTrainingList.subList(entity.getCurrentRow(), entity.getLastRow()));
			
			this.button("Super.Popup(5,'添加','/SCMDB/securityTrainingAction!newSecurityTraining.action','',450,350)",
					"Super.editData('/SCMDB/securityTrainingAction!findSecurityTraining.action?id=',450,350)",
					"Super.delData('/SCMDB/securityTrainingAction!delSecurityTraining.action?pId=')");
		} catch (Exception e) {
			log.error("findAll fail!reason:" + e.getMessage());
			return "fail";
		}
		
		return SUCCESS;
	}
	
	public String newSecurityTraining() {
		typeList = securityTrainingTypeService.getListSecurityTrainingType(null);
		
		return "newSucc";
	}
	
	private List<SecurityTrainingTypeBean> typeList;
	
	public List<SecurityTrainingTypeBean> getTypeList() {
		return typeList;
	}

//	public void setTypeList(List<SecurityTrainingTypeBean> typeList) {
//		this.typeList = typeList;
//	}

	public String findSecurityTraining() {
		try {
			String pId = request.getParameter("id");
			entity = securityTrainingService.getSecurityTraining(pId);
			
			typeList = securityTrainingTypeService.getListSecurityTrainingType(null);
			return "editSucc";
		} catch (Exception e) {
			log.error("find securityTraining failed! reason:" + e.getMessage());
			return "fail";
		}
	}
	
	public String editSecurityTraining() {
		try {
			User user = (User)session.get(CommonParamter.USER_ENTITY);
			entity.setUser(user);
			String result = securityTrainingService.editSecurityTraining(entity);
			messageSession(result, "安全培训交底");
			return "commitSucc";
		} catch (Exception e) {
			log.error("edit securityTraining failed! reason:" + e.getMessage());
			return "fail";
		}
	}
	
	public String delSecurityTraining() {
		String flg = null;
		try {
			String pId = request.getParameter("pId");
			if (StringUtils.isNotEmpty(pId)) {
				String[] arr = pId.split(",");
				flg = securityTrainingService.delSecurityTraining(arr);
			}
			
			messageSession(flg, "安全培训交底");
			log.info("delete suncce");
			return "commitSucc";
		} catch (Exception e) {
			log.error("delete securityTraining fail" + e.getMessage());
			return "fail";
		}		
	}
	
	public String addSecurityTraining() {
		try {
			List<File> files = getImage();
			if (files != null && files.size() > 0) {
	            for (int i = 0; i < files.size(); i++) {
	            	FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName().get(i));
	            	FileInputStream fis = new FileInputStream(files.get(i));
	                byte[] buffer = new byte[1024];
	                int len = 0;
	                while ((len = fis.read(buffer)) > 0) {
	                    fos.write(buffer, 0, len);
	                }
	                fis.close();
	                fos.close();
	            }
	        }
			
			User user = (User)session.get(CommonParamter.USER_ENTITY);
			entity.setUser(user);
			entity.setCreatedDate(new DateTime());
			String result = securityTrainingService.createSecurityTraining(entity);
			messageSession(result, "安全培训交底");
			//return "commitSucc";
			return SUCCESS;
		} catch (Exception e) {
			log.error("add securityTraining failed! reason:" + e.getMessage());
			return "fail";
		}
	}
	
	private List<File> image; // 上传的文件
    private List<String> imageFileName; // 文件名称
    private List<String> imageContentType; // 文件类型
    private String savePath;

	public List<File> getImage() {
		return image;
	}

	public void setImage(List<File> image) {
		this.image = image;
	}

	public List<String> getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(List<String> imageFileName) {
		this.imageFileName = imageFileName;
	}

	public List<String> getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(List<String> imageContentType) {
		this.imageContentType = imageContentType;
	}

	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
}
